package com.ecas.code.generator.dto;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Data
public class PageResultBean implements Serializable {
    private static final long serialVersionUID = -4355283544506529120L;

    private static final Integer DEFAULT_PAGE_NO=1;
    private static final Integer DEFAULT_PAGE_SIZE = 20;

    //总记录数
    private int totalCount;
    //每页记录数
    private Integer pageSize;
    //总页数
    private int totalPage;
    //当前页数
    private int pageNo;

    private transient List<?> list;

    public void setPageParams(Map<String,Object> pageParams) {
        if(pageParams == null) {
            throw new RuntimeException();
        }
        if(pageParams.get("pageSize") instanceof Integer) {
            pageSize = (Integer)pageParams.get("pageSize");
        }else{
            pageSize = DEFAULT_PAGE_SIZE;
        }
        if(pageParams.get("pageNo") instanceof Integer) {
            pageNo = (Integer)pageParams.get("pageNo");
        }else{
            pageNo = DEFAULT_PAGE_NO;
        }
         totalPage = totalCount/pageSize;
        totalPage =totalPage ==0?1:totalPage+1;
        int offset = (pageNo-1) * pageSize;
        pageParams.put("offset",offset);
        pageParams.put("limit",pageSize);
    }

}
